package db

import (
	"errors"
	"log"
	"server_cluster/battle/sharemem"
	"strconv"
	"strings"
)

type Config_skill_child struct {
	Id     int32  `gorm:"primary_key;column:id;autoIncrement:false" csv:"Id"`
	Type   int8   `gorm:"column:type" csv:"Type"`
	Effect string `gorm:"column:effect" csv:"Skill_Effect"`
}

func (tmp *Config_skill_child) TableName() string {
	return "config_skill_child"
}

func (tmp *Config_skill_child) getAll() ([]*Config_skill_child, error) {
	var data []*Config_skill_child
	result := Conn.Find(&data)

	return data, result.Error
}

func (tmp *Config_skill_child) LoadData() error {
	data, err := tmp.getAll()
	if err != nil {
		return err
	}
	var myshm = &(sharemem.Myshm.Cfg.Sklchd)
	for key, value := range data {
		if key >= sharemem.SKILL_CHILD_MAX {
			return errors.New("config_skill_child format error 1")
		}
		myshm[key].Id = value.Id
		myshm[key].Type = value.Type
		if len(value.Effect) > 0 {
			a_list := strings.Split(value.Effect, ";")
			if len(a_list) > sharemem.SKILL_CHILD_EFFECT_MAX {
				return errors.New("config_skill_child format error 6")
			}
			for k, v := range a_list {
				arg, err := strconv.Atoi(v)
				if err != nil {
					return errors.New("config_skill_child format error 7")
				}
				myshm[key].Effect[k] = int32(arg)
			}
		}
		//
	}

	log.Println("load config_skill_child completed.")

	return nil
}
